﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Benefit.Service.ServerData
{
    /// <summary>
    /// 持仓汇总
    /// </summary>
    public class ServiceT_PositionSummaryDelivery : Benefit.Interface.ServerData.IT_PositionSummaryDelivery
    {
        Benefit.DB.DBManager db = null;
        public ServiceT_PositionSummaryDelivery(Benefit.DB.DBManager db)
        {
            this.db = db;
        }


        /// <summary>
        /// 查询结算后的持仓汇总情况
        /// </summary>
        /// <param name="accountid"></param>
        /// <param name="pdate"></param>
        /// <returns></returns>
        public System.Collections.Generic.List<Models.ServerData.T_PositionSummaryDelivery> GetInfo(string pdate)
        {
            List<Models.ServerData.T_PositionSummaryDelivery> tpsd = new List<Models.ServerData.T_PositionSummaryDelivery>();
            Interface.ServerData.IT_Settle_History tsh = new ServerData.ServiceT_Settle_History(db);
            //T_Settle_History tsh = new T_Settle_History();
            int settlementid = tsh.GetDayMaxSettlementInfoId(pdate);
            if (settlementid != -1)
            {
                tpsd = GetPositionSummaryHistory(settlementid, pdate);
            }
            return tpsd;
        }

        /// <summary>
        /// 查询某天最大结算编号的持仓汇总
        /// </summary>
        /// <param name="settlementid">结算编号</param>
        /// <param name="pdate">日期</param>
        /// <returns></returns>
        public System.Collections.Generic.List<Models.ServerData.T_PositionSummaryDelivery> GetPositionSummaryHistory(int settlementid, string pdate)
        {
            DavidDB.ADO.DBManager dbmanager = new DavidDB.ADO.DBManager();
            System.Collections.Generic.List<Models.ServerData.T_PositionSummaryDelivery> tpsd = new System.Collections.Generic.List<Models.ServerData.T_PositionSummaryDelivery>();
            string sql = "select * from T_PositionSummaryDelivery where SettlementId='" + settlementid + "' and tradingday='" + pdate + "'";
            dynamic sdr = dbmanager.ExecuteReader(sql);
            while (sdr.Read())
            {
                Models.ServerData.T_PositionSummaryDelivery s = null;
                s = new Models.ServerData.T_PositionSummaryDelivery();
                s.AccountId = Convert.ToInt32(sdr["AccountId"]);
                s.BrokerId = Convert.ToString(sdr["BrokerId"]);
                s.Datetime = Convert.ToDouble(sdr["Datetime"]);
                s.DeliveryDate = Convert.ToString(sdr["DeliveryDate"]);
                s.HedgeFlag = Convert.ToString(sdr["HedgeFlag"]);
                s.Id = Convert.ToInt32(sdr["Id"]);
                s.InstrumentId = Convert.ToString(sdr["InstrumentId"]);
                s.InvestorId = Convert.ToString(sdr["InvestorId"]);
                s.LongAvgprice = Convert.ToDouble(sdr["LongAvgprice"]);
                s.LongCommission = Convert.ToDouble(sdr["LongCommission"]);
                s.LongMargin = Convert.ToDouble(sdr["LongMargin"]);
                s.LongPosition = Convert.ToInt32(sdr["LongPosition"]);
                s.Margin = Convert.ToDouble(sdr["Margin"]);
                s.PositionProfit = Convert.ToDouble(sdr["PositionProfit"]);
                s.ProductId = Convert.ToString(sdr["ProductID"]);
                s.SettlementPrice = Convert.ToDouble(sdr["SettlementPrice"]);
                s.ShortAvgprice = Convert.ToDouble(sdr["ShortAvgprice"]);
                s.ShortCommission = Convert.ToDouble(sdr["ShortCommission"]);
                s.ShortMargin = Convert.ToDouble(sdr["ShortMargin"]);
                s.ShortPosition = Convert.ToInt32(sdr["ShortPosition"]);
                s.YesterdaySettlement = Convert.ToDouble(sdr["YesterdaySettlement"]);
                s.PositionProfit2 = Convert.ToDouble(sdr["PositionProfit2"]);
                s.SettleMentID = Convert.ToInt32(sdr["SettleMentID"]);
                s.TradingDay = sdr["TradingDay"].ToString();
                tpsd.Add(s);
            }
            sdr.Close();
            dbmanager.Commit();
            dbmanager.Dispose();
            return tpsd;
        }

        /// <summary>
        /// 每天都需要执行的把Server中的数据导入到分析系统中的方法
        /// </summary>
        /// <param name="pdate">日期</param>
        public void CopyServerPositionSummaryDataIntoBenefit(string pdate)
        {
            Interface.ServerData.IT_PositionSummaryDelivery h = new ServerData.ServiceT_PositionSummaryDelivery(db);
            //Data.ServerData.T_PositionSummaryDelivery h = new Data.ServerData.T_PositionSummaryDelivery();
            List<Models.ServerData.T_PositionSummaryDelivery> tsm = h.GetInfo(pdate);
            foreach (Models.ServerData.T_PositionSummaryDelivery ha in tsm)
            {
                db.PostionSummaryDelivery.Add(ha);
            }
            db.SaveChanges();
        }


        /// <summary>
        /// 删除某日的所有持仓汇总记录
        /// </summary>
        /// <param name="pdate"></param>
        public void DeleteDayPositionSummaryFromBenefit(string pdate)
        {
            var historys = from t in db.PostionSummaryDelivery where t.TradingDay.Equals(pdate) select t;
            foreach (Models.ServerData.T_PositionSummaryDelivery ha in historys)
            {
                db.PostionSummaryDelivery.Remove(ha);
            }
            db.SaveChanges();
        }
    }
}
